import * as Sequelize from "sequelize";
import { Application } from "egg";

interface PackageAttributes {
  version_id: string;
  id: string;
  package_name: string;
  status: string;
  next_status?: string;
  src: string;
  remark?: string;
  refuse_reason?: string;
  create_user_id: string;
  modify_user_id: string;
  create_time: string;
  modify_time: string;
}

type PackageInstance = Sequelize.Instance<PackageAttributes> &
  PackageAttributes;

export default (app: Application) => {
  const { STRING, CHAR, UUID, UUIDV4, DATE, BLOB, NOW } = app.Sequelize;
  const attributes: SequelizeAttributes<PackageAttributes> = {
    version_id: { type: UUID, allowNull: false },
    id: {
      type: UUID,
      primaryKey: true,
      allowNull: false,
      defaultValue: UUIDV4
    },
    package_name: { type: STRING(50), allowNull: false },
    status: { type: CHAR(3), allowNull: false, defaultValue: "000" },
    next_status: { type: CHAR(3), allowNull: false, defaultValue: "000" },
    src: { type: STRING(255), allowNull: false },
    remark: { type: STRING(255), allowNull: true },
    refuse_reason: { type: STRING(255), allowNull: true },
    create_user_id: { type: UUID, allowNull: false },
    modify_user_id: { type: UUID, allowNull: false },
    create_time: { type: DATE, allowNull: false, defaultValue: NOW },
    modify_time: { type: DATE, allowNull: false, defaultValue: NOW }
  };
  const model = app.model.define<PackageInstance, PackageAttributes>(
    "Package",
    attributes,
    {
      tableName: "t_version_package"
    }
  );
  model.associate = () => {
    app.model.models.Package.belongsTo(app.model.models.Version, {
      foreignKey: "version_id",
      targetKey: "id"
    });
  };
  return model;
};
